<?php

namespace App\Models\Company;

use Illuminate\Database\Eloquent\Model;

class BackstageCategory extends Model
{
    protected $table = 'company_category';

    protected $fillable = [
        'name',
        'uri',
        'keys',
        'title',
        'describe'
    ];

    public function material()
    {
        $city = config('region.region.model', '');
        return $this->hasMany(Material::class, 'category_id', 'id')
            ->where(function ($query) use ($city) {
                if ($city && !is_domain_top()) {
                    $query->where('region_id', $city->id);
                }
                $query->where('is_state', true);
            })->orderBy('updated_at', 'desc')->limit(8);
    }

    public function whereMaterial()
    {
        $city = config('region.region.model', '');
        return $this->material()->with('region')->where(function ($query) use ($city) {
            if ($city && !is_domain_top()) {
                $query->where('region_id', $city->id);
            }
            $query->where('is_state', true);
        })->orderBy('updated_at', 'desc')->limit(8);
    }

    public function takeMaterial()
    {
        $city = config('region.region.model', '');
        return $this->hasMany(Material::class, 'category_id', 'id')
            ->where(function ($query) use ($city) {
                if ($city && !is_domain_top()) {
                    $query->where('region_id', $city->id);
                }else{
                    $query->where('is_top', true);
                }
                $query->where('is_state', true);
            })->orderBy('updated_at', 'desc')->limit(20);
    }

    public function furniture()
    {
        $city = config('region.region.model', '');
        return $this->hasMany(Furniture::class, 'category_id', 'id')
            ->where(function ($query) use ($city) {
                if ($city && !is_domain_top()) {
                    $query->where('region_id', $city->id);
                }
                $query->where('is_state', true);
            })->orderBy('updated_at', 'desc')->limit(220);
    }
}
